using System;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class StatsManager : MonoBehaviour
{
	[SerializeField]
	private int _maxHealth = 100;

	[SerializeField]
	private int _maxExperience = 100;

	[SerializeField]
	private int _maxArmor;

	[SerializeField]
	private int _maxEntityHealth;

	[SerializeField]
	private UILabel healthLabel;

	[SerializeField]
	private UILabel armorLabel;

	[SerializeField]
	private UILabel experianceLabel;

	[SerializeField]
	private UILabel lvlLabel;

	[SerializeField]
	private UILabel entityHealthLabel;

	[SerializeField]
	private UISprite healthBar;

	[SerializeField]
	private UISprite armorBar;

	[SerializeField]
	private UISprite experienceBar;

	[SerializeField]
	private UISprite entityHealthBar;

	[SerializeField]
	private UISprite backgroundHealthBar;

	[SerializeField]
	private UISprite iconHeart;

	[SerializeField]
	private UIWidget experienceWidget;

	[SerializeField]
	private UIWidget experienceBarBG;

	[SerializeField]
	private float experienceWidgetFadeTime = 3f;

	[SerializeField]
	private Animator _animator;

	[SerializeField]
	private Color colorBarBackgroundForZombie;

	[SerializeField]
	private Color colorBarForZombie;

	[SerializeField]
	private Color colorHeart;

	public GameObject armorGameObject;

	public GameObject healthgameObject;

	public GameObject entityGameObject;

	private int lastHelth;

	private int lastArmor;

	private int lastExperience;

	private int lastEntityHealth;

	private int lastLvL;

	private bool init = true;

	private Animator animator
	{
		get
		{
			if (_animator == null)
			{
				_animator = GetComponent<Animator>();
				_animator.logWarnings = false;
			}
			return _animator;
		}
	}

	public int maxHealth
	{
		set
		{
			_maxHealth = value;
			health = lastHelth;
		}
	}

	public int maxArmor
	{
		set
		{
			_maxArmor = value;
			bool flag = GameController.instance != null && GameController.instance.playerScript != null && GameController.instance.playerScript.inCar;
			armorGameObject.SetActive(_maxArmor > 0 && !flag);
		}
	}

	public int maxExperience
	{
		set
		{
			_maxExperience = value;
			experience = lastExperience;
		}
	}

	public int health
	{
		set
		{
			if ((bool)healthBar)
			{
				healthBar.fillAmount = (float)value / (float)_maxHealth;
			}
			if ((bool)healthLabel)
			{
				healthLabel.text = value.ToString();
			}
			lastHelth = value;
		}
	}

	public int armor
	{
		set
		{
			lastArmor = value;
			if ((bool)armorLabel)
			{
				armorLabel.text = value.ToString();
			}
			if ((bool)armorBar)
			{
				armorBar.fillAmount = (float)value / (float)_maxArmor;
			}
			bool flag = GameController.instance != null && GameController.instance.playerScript != null && GameController.instance.playerScript.inCar;
			armorGameObject.SetActive(value > 0 && !flag);
		}
	}

	public int lvl
	{
		set
		{
			if (lastLvL != value)
			{
				Animator component = experienceWidget.GetComponent<Animator>();
				if ((bool)component && lastLvL != 0)
				{
					component.SetTrigger("LevelUp");
				}
				if ((bool)lvlLabel)
				{
					lvlLabel.text = value.ToString();
				}
				lastLvL = value;
			}
		}
	}

	public int entityHealth
	{
		set
		{
			lastEntityHealth = value;
			if ((bool)entityHealthLabel)
			{
				entityHealthLabel.text = value.ToString();
			}
			if ((bool)entityHealthBar)
			{
				entityHealthBar.fillAmount = (float)lastEntityHealth / (float)_maxEntityHealth;
			}
		}
	}

	public int maxEntityHealth
	{
		set
		{
			_maxEntityHealth = value;
			if ((bool)entityHealthBar)
			{
				entityHealthBar.fillAmount = (float)lastEntityHealth / (float)_maxEntityHealth;
			}
		}
	}

	public int experience
	{
		set
		{
			if (lastExperience == value)
			{
				return;
			}
			float cPercent = (float)value / (float)_maxExperience;
			float lastPercent = (float)lastExperience / (float)_maxExperience;
			if (!init)
			{
				if ((bool)experianceLabel)
				{
					experianceLabel.text = value.ToString();
				}
				experienceWidget.alpha = 0.4f;
				HOTween.Kill(experienceWidget);
				if ((bool)experienceBarBG)
				{
					HOTween.To(experienceBarBG, 0.5f, new TweenParms().Prop("alpha", 1).OnComplete((TweenDelegate.TweenCallback)delegate
					{
						HOTween.To(experienceBarBG, 0.2f, "alpha", 0).easeType = EaseType.Linear;
					}));
				}
				HOTween.To(experienceWidget, experienceWidgetFadeTime / 6f, new TweenParms().Prop("alpha", 1).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					if (cPercent > lastPercent)
					{
						HOTween.To(experienceBar, experienceWidgetFadeTime / 4f, new TweenParms().Prop("fillAmount", cPercent).OnComplete((TweenDelegate.TweenCallback)delegate
						{
							HOTween.To(experienceWidget, experienceWidgetFadeTime / 2f, "alpha", 0).easeType = EaseType.Linear;
						}));
					}
					else
					{
						HOTween.To(experienceBar, experienceWidgetFadeTime / 8f, new TweenParms().Prop("fillAmount", 1).OnComplete((TweenDelegate.TweenCallback)delegate
						{
							experienceBar.fillAmount = 0f;
							HOTween.To(experienceBar, experienceWidgetFadeTime / 8f, new TweenParms().Prop("fillAmount", cPercent).OnComplete((TweenDelegate.TweenCallback)delegate
							{
								HOTween.To(experienceWidget, experienceWidgetFadeTime / 2f, "alpha", 0).easeType = EaseType.Linear;
							}));
						}));
					}
				}));
			}
			else
			{
				init = false;
				experienceBar.fillAmount = cPercent;
			}
			lastExperience = value;
		}
	}

	private void OnEnable()
	{
		if (GameController.instance.curTypeGame == TypeGame.RocketLeague)
		{
			base.gameObject.SetActive(false);
		}
	}

	private void Awake()
	{
		if (GameController.instance.curTypeGame == TypeGame.Zombe)
		{
			healthLabel.gameObject.SetActive(false);
			armorLabel.gameObject.SetActive(false);
			backgroundHealthBar.color = colorBarBackgroundForZombie;
		}
		iconHeart.color = colorHeart;
		healthBar.color = colorHeart;
		SwitchHealthBarToEntity(false);
		experienceWidget.alpha = 0f;
		lvlLabel.text = string.Empty;
	}

	public void ChangeHealth()
	{
		if (settings.offlineMode)
		{
			healthBar.color = colorHeart;
		}
		else if (PlayerBehavior.MyPlayer.localTeam == PunTeams.Team.red && GameController.instance.curTypeGame == TypeGame.Zombe)
		{
			healthBar.color = colorBarForZombie;
			iconHeart.color = colorBarForZombie;
		}
		else if (PlayerBehavior.MyPlayer.localTeam == PunTeams.Team.blue && GameController.instance.curTypeGame == TypeGame.Zombe)
		{
			healthBar.color = colorHeart;
			iconHeart.color = colorHeart;
		}
	}

	public void SwitchHealthBarToEntity(bool value)
	{
		if (healthgameObject != entityGameObject)
		{
			healthgameObject.SetActive(!value);
			armorGameObject.SetActive(!value && Convert.ToInt32(armorLabel.text) > 0);
			entityGameObject.SetActive(value);
		}
	}

	public void DisableAllHealthBars()
	{
		healthgameObject.SetActive(false);
		armorGameObject.SetActive(false);
		entityGameObject.SetActive(false);
	}
}
